﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.BIM;
using YArchitech.LIB;

namespace YArchitech.DataModel
{
	public class GarageElectricalRoomAuxiliary
	{
		public GarageElectricalRoomAuxiliary()
		{
			if (!File.Exists(this.xmlFilePath))
			{
				File.AppendAllText(this.xmlFilePath, File.ReadAllText(this.backUpXmlFilePath));
			}
			this.xmlDoc = new XmlDocument();
			this.xmlDoc.Load(this.xmlFilePath);
			this.xmlNode = this.xmlDoc.DocumentElement;
			this.FileContainData();
		}

		public void FileContainData()
		{
			if (this.xmlNode.ChildNodes.Count > 0)
			{
				return;
			}
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.backUpXmlFilePath);
			XmlNode documentElement = xmlDocument.DocumentElement;
			if (documentElement != null)
			{
				foreach (object obj in documentElement.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					XmlElement xmlElement = this.xmlDoc.CreateElement("RoomKeyword");
					xmlElement.SetAttribute("KID", xmlNode.Attributes["KID"].Value);
					xmlElement.SetAttribute("KeyName", xmlNode.Attributes["KeyName"].Value);
					xmlElement.SetAttribute("TypeID", xmlNode.Attributes["TypeID"].Value);
					xmlElement.SetAttribute("TypeName", xmlNode.Attributes["TypeName"].Value);
					this.xmlNode.AppendChild(xmlElement);
				}
				this.xmlDoc.Save(this.xmlFilePath);
			}
		}

		public List<ElectricalRoomType> LoadRoomTypeData()
		{
			List<ElectricalRoomType> list = new List<ElectricalRoomType>();
			foreach (object obj in this.xmlNode.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				list.Add(new ElectricalRoomType
				{
					KID = xmlNode.Attributes["KID"].Value,
					KeyName = xmlNode.Attributes["KeyName"].Value,
					TypeID = xmlNode.Attributes["TypeID"].Value,
					TypeName = xmlNode.Attributes["TypeName"].Value
				});
			}
			return list;
		}

		public ElectricalRoomType GetBackupRoomTypeDefaultData()
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(this.backUpXmlFilePath);
			XmlNode documentElement = xmlDocument.DocumentElement;
			return new ElectricalRoomType
			{
				KID = documentElement.ChildNodes[0].Attributes["KID"].Value,
				KeyName = documentElement.ChildNodes[0].Attributes["KeyName"].Value,
				TypeID = documentElement.ChildNodes[0].Attributes["TypeID"].Value,
				TypeName = documentElement.ChildNodes[0].Attributes["TypeName"].Value
			};
		}

		public void SaveSpaceKeyword(List<ElectricalRoomType> listRoomType)
		{
			this.xmlDoc.DocumentElement.RemoveAll();
			foreach (ElectricalRoomType electricalRoomType in listRoomType)
			{
				XmlElement xmlElement = this.xmlDoc.CreateElement("RoomKeyword");
				xmlElement.SetAttribute("KID", electricalRoomType.KID);
				xmlElement.SetAttribute("KeyName", electricalRoomType.KeyName);
				xmlElement.SetAttribute("TypeID", electricalRoomType.TypeID);
				xmlElement.SetAttribute("TypeName", electricalRoomType.TypeName);
				this.xmlNode.AppendChild(xmlElement);
			}
			this.xmlDoc.Save(this.xmlFilePath);
		}

		private XmlDocument xmlDoc;

		private XmlNode xmlNode;

		private string backUpXmlFilePath = Path.Combine(Product.DataLocation, "RoomKeyword.xml");

		private string xmlFilePath = Path.Combine(Product.UserDataLocation, "RoomKeyword.xml");
	}
}
